<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/11 0011
 * Time: 下午 11:59
 */
namespace api\controllers;

use common\models\BaseModels;
use common\models\kaoguobao\BenefitImg;
use common\models\kaoguobao\Order;
use common\services\config\Config;
use common\services\HttpUploadService;

use Yii;

class FileController extends BaseController
{
    /**
     * base64接口入口
     * orderId          订单ID
     * filePath         base64编码格式的图片
     * @return bool
     */
    public function actionBaseImgSet()
    {
        $orderId      = trim($this->request->post('order_id'));
        $cardImgUrl   = trim($this->request->post('card_img_url'));
        $resultImgUrl = trim($this->request->post('result_img_url'));
        $key          = trim($this->request->post('key'));
        if(empty($orderId)){
            return $this->errorResult(Config::$notOrderId);
        }
        if(empty($cardImgUrl) || empty($resultImgUrl) || empty($key)){
            return $this->errorResult(Config::$parameter);
        }
        $filePath = [
            'cardImgUrl' => $cardImgUrl,
            'resultImgUrl' => $resultImgUrl
        ];

        $urlArray = [];
        // 上传后的图片名称
        $transaction = Yii::$app->db_kgb->beginTransaction();
        try {
            foreach ($filePath as $k => $value){
                $keyName = 'QX'.$orderId.$k.date('YmdHis');
                $imgUrl = HttpUploadService::qiNiuBase64PictureUpload($keyName,$value);
                if($imgUrl == false){
                    return $this->errorResult(Config::$fileFail);
                }
                $urlArray[$k] = $imgUrl;
            }
            if(is_array($urlArray) && $urlArray){
                $model = BenefitImg::find()->where(['order_id' => $orderId])->limit(1)->one();
                if(empty($model)){
                    $model              = new BenefitImg();
                    $model->order_id    = $orderId;
                }
                $model->card_img_url    = $urlArray['cardImgUrl'];
                $model->result_img_url  = $urlArray['resultImgUrl'];
                $model->key             = $key;
                if($model->save()){
                    // 图片保存成功之后请求订单更新状态
                    $orderModel = Order::find()->where(['id' => $orderId])->limit(1)->one();
                    if(empty($orderModel)){
                        return $this->errorResult(Config::$notOrder);
                    }
                    $orderModel->order_status = Order::ORDER_STATUS_UP_PROOF;
                    if($orderModel->save()){
                        $transaction->commit();
                        return $this->errorResult(Config::$ok);
                    }
                }else{
                    return $this->errorResult(Config::$fileFail);
                }
            }
            return $this->errorResult(Config::$fileFail);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return ['code' => -1001 , 'msg' => $e->getMessage()];
        }
    }
}